package com.yunchang.springboot.utils.amazon;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
//import com.yunchang.springboot.scheduled.mwsapi.AmazonMwsApiSchedule;
import com.yunchang.springboot.scheduled.mwsreport.ReportBase;
import com.yunchang.springboot.utils.CryptoUtil;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.util.*;

import static com.yunchang.springboot.utils.DateUtil.*;

/**
 * @author wanglongwei
 * @data 2019/7/15 11:54
 */
@Slf4j
public class AmazonMwsApi extends ReportBase {

    private static final String SUCCESS_GET_REPORT_LIST_RESPONSE = "GetReportListResponse";
    //public static String FINANCIALEVENTGROUPSTARTEDBEFORE ="FinancialEventGroupStartedBefore";

    private static String FINANCIALEVENTGROUPSTARTEDAFTER = "FinancialEventGroupStartedAfter";

    /**
     * 报告接口流程所需要的参数
     */
    private static final List<String> PUBLIC_REPORT_PARAM = Arrays.asList("SellerId", "MWSAuthToken", "AWSAccessKeyId", "SecretKey", "mwsUrl", "taskName", "shopName", "areaCode", "ReportType");
    /**
     * 发送GetReportList所需参数
     */
    private static final List<String> GET_REPORT_LIST_PARAM = Arrays.asList("SellerId", "MWSAuthToken", "AWSAccessKeyId", "SecretKey", "ReportType", "mwsUrl", "ReportRequestIdList.Id.1");

    /**
     * 发送RequestReport所需参数
     */
    private static final List<String> REQUEST_REPORT_PARAM = Arrays.asList("SellerId", "MWSAuthToken", "AWSAccessKeyId", "SecretKey", "ReportType", "mwsUrl", "StartDate", "EndDate", "MarketplaceIdList.Id.1");

    /**
     * 发送GetReport所需参数
     */
    private static final List<String> GET_REPORT_PARAM = Arrays.asList("SellerId", "MWSAuthToken", "AWSAccessKeyId", "SecretKey", "ReportId", "mwsUrl");

    private static final String SECRETKEY_PARAM = "SecretKey";

    private static final String MWSURL_PARAM = "mwsUrl";

    private static final String REPORT_TYPE = "ReportType";


//    public static List<List<String>> registerDestinationOnce(String mwsUrl, String secretKey, Map<String, String> baseParams, Map<String, String> otherParams) {
//        if (StringUtil.isBlank(mwsUrl)) {
//            log.warn("mwsUrl不能为空");
//            return null;
//        }
//        if (StringUtil.isBlank(secretKey)) {
//            log.warn("secretKey不能为空");
//            return null;
//        }
//        if (null == baseParams) {
//            log.warn("baseParams不能为空");
//            return null;
//        }
////        if (StringUtil.isBlank(requestType)) {
////            log.warn("requestType不能为空");
////            return null;
////        }
////        baseParams.put("ReportType", requestType);
//        if (null != otherParams) {
//            baseParams.putAll(otherParams);
//        }
//        String ReportRequestId = null;
//
//        //发送RequestReport请求
//        Map<String, Object> requestReport = AmazonMwsUntil.clientXML("RegisterDestination", mwsUrl, secretKey, baseParams);
//        List<List<String>> requestReportList = AmazonMwsUntil.clientTXT("RegisterDestination", mwsUrl, secretKey, baseParams);
//        if (null != requestReportList || requestReportList.size() > 0) {
//            return requestReportList;
//        }
////        if (null == requestReport || null != requestReport.get(AmazonMwsUntil.ERROR_RESPONSE)) {
////            log.warn("获取Report请求出错：requestReport" + requestType + "---requestReport详情:" + requestReport);
////            return null;
////        }
////        else {
//        Map<String, Object> RequestReportResponse = (Map<String, Object>) requestReport.get("RequestReportResponse");
//        Map<String, Object> RequestReportResult = (Map<String, Object>) RequestReportResponse.get("RequestReportResult");
//        Map<String, Object> ReportRequestInfo = (Map<String, Object>) RequestReportResult.get("ReportRequestInfo");
//        ReportRequestId = String.valueOf(ReportRequestInfo.get("ReportRequestId"));
////        }
//        //去掉多余参数防止签名计算错误
//        baseParams.remove(AmazonMwsApiSchedule.PUBLIC_PARAM_MARKETPLACE_ID_LIST_ID);
//        baseParams.remove("ReportType");
////        baseParams.put("ReportTypeList.Type.1", AmazonMwsUntil.urlEncode(requestType));
//        if (null != otherParams) {
//            for (String key : otherParams.keySet()) {
//                baseParams.remove(key);
//            }
//        }
//        try {
//            Thread.sleep(100000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        int j = 0;
//        Boolean work = true;
//        while (work) {
//            //发送GetReportList请求
//            Map<String, Object> getReportList = AmazonMwsUntil.clientXML("GetReportList", mwsUrl, secretKey, baseParams);
//            if (null == getReportList || null != getReportList.get(AmazonMwsUntil.ERROR_RESPONSE)) {
//                log.warn("获取Report请求出错getReportList" + getReportList);
//                return null;
//            }
//            if (null == getReportList.get(SUCCESS_GET_REPORT_LIST_RESPONSE)) {
//                log.warn("获取Report请求出错getReportList:GetReportListResponse" + getReportList);
//                return null;
//            }
//            //去掉多余参数防止签名计算错误
//            baseParams.remove("ReportTypeList.Type.1");
//            Map<String, Object> map1 = (Map<String, Object>) getReportList.get(SUCCESS_GET_REPORT_LIST_RESPONSE);
//            String getReportListResult = "GetReportListResult";
//            if (null == map1.get(getReportListResult)) {
//                log.warn("获取Report请求出错getReportList:GetReportListResponse:GetReportListResult" + getReportList);
//                return null;
//            }
//            Map<String, Object> map2 = (Map<String, Object>) map1.get(getReportListResult);
//            String reportInfo = "ReportInfo";
//            //获取reportInfo对象，-----亚马逊当返回reportInfo只有一个的时候为map，多个的时候为数组
//            Object reportInfoObject = map2.get(reportInfo);
//            if (null == reportInfoObject) {
//                log.warn("获取Report请求出错getReportList:GetReportListResponse:GetReportListResult:ReportInfo" + getReportList);
//                return null;
//            }
//            Map<String, Object> map3;
//            if (reportInfoObject.getClass().isArray() || reportInfoObject instanceof JSONArray || reportInfoObject instanceof JSON) {
//                if (reportInfoObject instanceof JSONObject) {
//                    map3 = (Map<String, Object>) reportInfoObject;
//                } else {
//                    List<Map<String, Object>> list = (List<Map<String, Object>>) reportInfoObject;
//                    String reportId = null;
//                    String ReportRequestId1 = null;
//                    for (int i = 0; i < list.size(); i++) {
//                        map3 = list.get(i);
//                        ReportRequestId1 = String.valueOf(map3.get("ReportRequestId"));
//                        if (ReportRequestId.equals(ReportRequestId1)) {
//                            //发送GetReport请求
//                            reportId = String.valueOf(map3.get("ReportId"));
//                            baseParams.put("ReportId", reportId);
//                            return AmazonMwsUntil.clientTXT("GetReport", mwsUrl, secretKey, baseParams);
//                        }
//                    }
//                    j++;
//                    try {
//                        Thread.sleep(100000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    if (j > 2) {
//                        work = false;
//                    }
//                }
//            } else {
//                map3 = (Map<String, Object>) reportInfoObject;
//            }
//        }
//        return null;
////        return getReport(mwsUrl, secretKey, baseParams, requestType, null);
//    }
//
//    public static List<List<String>> getReport(String mwsUrl, String secretKey, Map<String, String> baseParams, String requestType) {
//        return getReport(mwsUrl, secretKey, baseParams, requestType, null);
//    }
//
//    public static List<List<String>> getReport(String mwsUrl, String secretKey, Map<String, String> baseParams, String requestType, Map<String, String> otherParams) {
//        if (StringUtil.isBlank(mwsUrl)) {
//            log.warn("mwsUrl不能为空");
//            return null;
//        }
//        if (StringUtil.isBlank(secretKey)) {
//            log.warn("secretKey不能为空");
//            return null;
//        }
//        if (null == baseParams) {
//            log.warn("baseParams不能为空");
//            return null;
//        }
//        if (StringUtil.isBlank(requestType)) {
//            log.warn("requestType不能为空");
//            return null;
//        }
//        baseParams.put("ReportType", requestType);
//        if (null != otherParams) {
//            baseParams.putAll(otherParams);
//        }
//        String ReportRequestId = null;
//
//        //发送RequestReport请求
//        Map<String, Object> requestReport = AmazonMwsUntil.clientXML("RequestReport", mwsUrl, secretKey, baseParams);
//        if (null == requestReport || null != requestReport.get(AmazonMwsUntil.ERROR_RESPONSE)) {
//            log.warn("获取Report请求出错：requestReport" + requestType + "---requestReport详情:" + requestReport);
//            return null;
//        } else {
//            Map<String, Object> RequestReportResponse = (Map<String, Object>) requestReport.get("RequestReportResponse");
//            Map<String, Object> RequestReportResult = (Map<String, Object>) RequestReportResponse.get("RequestReportResult");
//            Map<String, Object> ReportRequestInfo = (Map<String, Object>) RequestReportResult.get("ReportRequestInfo");
//            ReportRequestId = String.valueOf(ReportRequestInfo.get("ReportRequestId"));
//        }
//        //去掉多余参数防止签名计算错误
//        baseParams.remove(AmazonMwsApiSchedule.PUBLIC_PARAM_MARKETPLACE_ID_LIST_ID);
//        baseParams.remove("ReportType");
//        baseParams.put("ReportTypeList.Type.1", AmazonMwsUntil.urlEncode(requestType));
//        if (null != otherParams) {
//            for (String key : otherParams.keySet()) {
//                baseParams.remove(key);
//            }
//        }
//        try {
//            Thread.sleep(100000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        int j = 0;
//        Boolean work = true;
//        while (work) {
//            //发送GetReportList请求
//            Map<String, Object> getReportList = AmazonMwsUntil.clientXML("GetReportList", mwsUrl, secretKey, baseParams);
//            if (null == getReportList || null != getReportList.get(AmazonMwsUntil.ERROR_RESPONSE)) {
//                log.warn("获取Report请求出错getReportList" + getReportList);
//                return null;
//            }
//            if (null == getReportList.get(SUCCESS_GET_REPORT_LIST_RESPONSE)) {
//                log.warn("获取Report请求出错getReportList:GetReportListResponse" + getReportList);
//                return null;
//            }
//            //去掉多余参数防止签名计算错误
//            baseParams.remove("ReportTypeList.Type.1");
//            Map<String, Object> map1 = (Map<String, Object>) getReportList.get(SUCCESS_GET_REPORT_LIST_RESPONSE);
//            String getReportListResult = "GetReportListResult";
//            if (null == map1.get(getReportListResult)) {
//                log.warn("获取Report请求出错getReportList:GetReportListResponse:GetReportListResult" + getReportList);
//                return null;
//            }
//            Map<String, Object> map2 = (Map<String, Object>) map1.get(getReportListResult);
//            String reportInfo = "ReportInfo";
//            //获取reportInfo对象，-----亚马逊当返回reportInfo只有一个的时候为map，多个的时候为数组
//            Object reportInfoObject = map2.get(reportInfo);
//            if (null == reportInfoObject) {
//                log.warn("获取Report请求出错getReportList:GetReportListResponse:GetReportListResult:ReportInfo" + getReportList);
//                return null;
//            }
//            Map<String, Object> map3;
//            if (reportInfoObject.getClass().isArray()
//                    || reportInfoObject instanceof JSONArray
//                    || reportInfoObject instanceof JSON) {
//                if (reportInfoObject instanceof JSONObject) {
//                    map3 = (Map<String, Object>) reportInfoObject;
//                } else {
//                    List<Map<String, Object>> list = (List<Map<String, Object>>) reportInfoObject;
//                    String reportId = null;
//                    String ReportRequestId1 = null;
//                    for (int i = 0; i < list.size(); i++) {
//                        map3 = list.get(i);
//                        ReportRequestId1 = String.valueOf(map3.get("ReportRequestId"));
//                        if (ReportRequestId.equals(ReportRequestId1)) {
//                            //发送GetReport请求
//                            reportId = String.valueOf(map3.get("ReportId"));
//                            baseParams.put("ReportId", reportId);
//                            return AmazonMwsUntil.clientTXT("GetReport", mwsUrl, secretKey, baseParams);
//                        }
//                    }
//                    j++;
//                    try {
//                        Thread.sleep(100000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    if (j > 2) {
//                        work = false;
//                    }
//                }
//            } else {
//                map3 = (Map<String, Object>) reportInfoObject;
//            }
//        }
//        return null;
//    }

//    public static List<List<String>> getReport2(String mwsUrl, String secretKey, Map<String, String> baseParams, String requestType, Map<String, String> otherParams) {
//        if (StringUtil.isBlank(mwsUrl)) {
//            log.warn("mwsUrl不能为空");
//            return null;
//        }
//        if (StringUtil.isBlank(secretKey)) {
//            log.warn("secretKey不能为空");
//            return null;
//        }
//        if (null == baseParams) {
//            log.warn("baseParams不能为空");
//            return null;
//        }
//        if (StringUtil.isBlank(requestType)) {
//            log.warn("requestType不能为空");
//            return null;
//        }
//        baseParams.put("ReportType", requestType);
//        if (null != otherParams) {
//            baseParams.putAll(otherParams);
//        }
//        //发送RequestReport请求
//        Map<String, Object> requestReport = AmazonMwsUntil.clientXML("RequestReport", mwsUrl, secretKey, baseParams);
//        if (null == requestReport || null != requestReport.get(AmazonMwsUntil.ERROR_RESPONSE)) {
//            log.warn("获取Report请求出错：requestReport" + requestType + "---requestReport详情:" + requestReport);
//            return null;
//        }
//        //去掉多余参数防止签名计算错误
//        baseParams.remove(AmazonMwsApiSchedule.PUBLIC_PARAM_MARKETPLACE_ID_LIST_ID);
//        baseParams.remove("ReportType");
//        baseParams.put("ReportTypeList.Type.1", AmazonMwsUntil.urlEncode(requestType));
//        if (null != otherParams) {
//            for (String key : otherParams.keySet()) {
//                baseParams.remove(key);
//            }
//        }
//        //发送GetReportList请求
//        Map<String, Object> getReportList = AmazonMwsUntil.clientXML("GetReportList", mwsUrl, secretKey, baseParams);
//        if (null == getReportList || null != getReportList.get(AmazonMwsUntil.ERROR_RESPONSE)) {
//            log.warn("获取Report请求出错getReportList" + getReportList);
//            return null;
//        }
//        if (null == getReportList.get(SUCCESS_GET_REPORT_LIST_RESPONSE)) {
//            log.warn("获取Report请求出错getReportList:GetReportListResponse" + getReportList);
//            return null;
//        }
//        //去掉多余参数防止签名计算错误
//        baseParams.remove("ReportTypeList.Type.1");
//        Map<String, Object> map1 = (Map<String, Object>) getReportList.get(SUCCESS_GET_REPORT_LIST_RESPONSE);
//        String getReportListResult = "GetReportListResult";
//        if (null == map1.get(getReportListResult)) {
//            log.warn("获取Report请求出错getReportList:GetReportListResponse:GetReportListResult" + getReportList);
//            return null;
//        }
//        Map<String, Object> map2 = (Map<String, Object>) map1.get(getReportListResult);
//        String reportInfo = "ReportInfo";
//        //获取reportInfo对象，-----亚马逊当返回reportInfo只有一个的时候为map，多个的时候为数组
//        Object reportInfoObject = map2.get(reportInfo);
//        if (null == reportInfoObject) {
//            log.warn("获取Report请求出错getReportList:GetReportListResponse:GetReportListResult:ReportInfo" + getReportList);
//            return null;
//        }
//        Map<String, Object> map3;
//        if (reportInfoObject.getClass().isArray()
//                || reportInfoObject instanceof JSONArray
//                || reportInfoObject instanceof JSON) {
//            if (reportInfoObject instanceof JSONObject) {
//                map3 = (Map<String, Object>) reportInfoObject;
//            } else {
//                List<Map<String, Object>> list = (List<Map<String, Object>>) reportInfoObject;
//                map3 = list.get(0);
//            }
//        } else {
//            map3 = (Map<String, Object>) reportInfoObject;
//        }
//        String reportId = String.valueOf(map3.get("ReportId"));
//        //发送GetReport请求
//        baseParams.put("ReportId", reportId);
//        return AmazonMwsUntil.clientTXT("GetReport", mwsUrl, secretKey, baseParams);
//    }

    // payment 2019 11 22于payment返回数据
    public static Object getPaymentReport(String secretKey, Map<String, String> baseParams, String requestType, String nextToken) {
        if (StringUtil.isBlank(secretKey)) {
            log.warn("secretKey不能为空");
            return null;
        }
        if (null == baseParams) {
            log.warn("baseParams不能为空");
            return null;
        }
        if (null == baseParams) {
            log.warn("baseParams不能为空");
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        if (requestType.equals("ListFinancialEventGroups")) {
            baseParams.put(FINANCIALEVENTGROUPSTARTEDAFTER, AmazonMwsUntil.urlEncode(getISO8601Timestamp("+8")));
            map = AmazonMwsUntil.paymentClientXML(requestType, "https://mws.amazonservices.com/Finances/2015-05-01", secretKey, baseParams, "Finances/2015-05-01");
            baseParams.remove(FINANCIALEVENTGROUPSTARTEDAFTER);
            return map;
        }
        if (requestType.equals("ListFinancialEvents")) {
            baseParams.put("PostedAfter", AmazonMwsUntil.urlEncode(timeToISO8601((getFutureDateDay(-180)))));
            baseParams.put("PostedBefore", AmazonMwsUntil.urlEncode(timeToISO8601((getFutureDateDay(-1)))));
            map = AmazonMwsUntil.paymentClientXML(requestType, "https://mws.amazonservices.com/Finances/2015-05-01", secretKey, baseParams, "Finances/2015-05-01");
            baseParams.remove("PostedAfter");
            baseParams.remove("PostedBefore");
            return map;
        }
        if (requestType.equals("ListFinancialEventsByNextToken")) {
            baseParams.put("NextToken", AmazonMwsUntil.urlEncode(nextToken));
            Map<String, Object> result = AmazonMwsUntil.paymentClientXML(requestType, "https://mws.amazonservices.com/Finances/2015-05-01", secretKey, baseParams, "Finances/2015-05-01");
            baseParams.remove("NextToken");
            return result;
        }
        return 0;
    }

    /**
     * 发送requestReport请求获取RequestReportId
     *
     * @param baseParams
     * @return
     */
    public static Map<String, Map<String, String>> sendRequestReport(Map<String, String> baseParams) {
        // 存放全部的请求参数包括任务名称
        Map<String, String> allParam = new HashMap<>(baseParams.size());
        for (String param : PUBLIC_REPORT_PARAM) {
            if (baseParams.containsKey(param)) {
                allParam.put(param, baseParams.get(param));
            }
        }
        List<String> unnecessaryParam = new ArrayList();
        // 去除非RequestReport接口所需要的参数
        for (String key : baseParams.keySet()) {
            if (!REQUEST_REPORT_PARAM.contains(key)) {
                unnecessaryParam.add(key);
            }
        }
        for (String param : unnecessaryParam) {
            baseParams.remove(param);
        }
        if (StringUtil.isBlank(baseParams.get(MWSURL_PARAM))) {
            log.warn("mwsUrl不能为空");
            return null;
        }
        if (StringUtil.isBlank(baseParams.get(SECRETKEY_PARAM))) {
            log.warn("secretKey不能为空");
            return null;
        }
        if (null == baseParams) {
            log.warn("baseParams不能为空");
            return null;
        }
        if (StringUtil.isBlank(baseParams.get(REPORT_TYPE))) {
            log.warn("requestType不能为空");
            return null;
        }
        baseParams.put("ReportType", baseParams.get(REPORT_TYPE));
        Map<String, Map<String, String>> reportRequestAndParamMap = new HashMap<>();
        String ReportRequestId = null;

        //发送RequestReport请求
        Map<String, Object> requestReport = AmazonMwsUntil.clientXML("RequestReport", baseParams.get(MWSURL_PARAM), baseParams.get(SECRETKEY_PARAM), baseParams);
        if (null == requestReport || null != requestReport.get(AmazonMwsUntil.ERROR_RESPONSE)) {
            log.info("获取RequestReport请求出错：requestReport " + baseParams.get("ReportType") + "---requestReport详情:" + requestReport + " 任务名称为：" + allParam.get("taskName") + " 店铺名为：" + allParam.get("shopName") + " 区域名为：" + allParam.get("areaCode"));
            return null;
        } else {
            Map<String, Object> RequestReportResponse = (Map<String, Object>) requestReport.get("RequestReportResponse");
            Map<String, Object> RequestReportResult = (Map<String, Object>) RequestReportResponse.get("RequestReportResult");
            Map<String, Object> ReportRequestInfo = (Map<String, Object>) RequestReportResult.get("ReportRequestInfo");
            ReportRequestId = String.valueOf(ReportRequestInfo.get("ReportRequestId"));
            reportRequestAndParamMap.put(ReportRequestId, allParam);
        }
        return reportRequestAndParamMap;
    }


    /**
     * 发送GetReportList请求获取ReportId
     *
     * @param baseParams
     * @return
     */
    public static Map<String, Map<String, String>> sendGetReportList(Map<String, String> baseParams) {
        String ReportRequestId = baseParams.get("ReportRequestId");
        // 存放公用的请求参数包括任务名称
        Map<String, String> allParam = new HashMap<>(baseParams.size());
        for (String param : PUBLIC_REPORT_PARAM) {
            if (baseParams.containsKey(param)) {
                allParam.put(param, baseParams.get(param));
            }
        }
        List<String> unnecessaryParam = new ArrayList();
        // 去除非GetReportList接口所需要的参数
        for (String key : baseParams.keySet()) {
            if (!GET_REPORT_LIST_PARAM.contains(key)) {
                unnecessaryParam.add(key);
            }
        }
        for (String param : unnecessaryParam) {
            baseParams.remove(param);
        }
        baseParams.put("ReportTypeList.Type.1", AmazonMwsUntil.urlEncode(baseParams.get("ReportType")));
        baseParams.remove("ReportType");
        if (null == baseParams) {
            log.warn("params不能为空");
            return null;
        }
        if (StringUtil.isBlank(baseParams.get(MWSURL_PARAM))) {
            log.warn("mwsUrl不能为空");
            return null;
        }
        if (StringUtil.isBlank(baseParams.get(SECRETKEY_PARAM))) {
            log.warn("secretKey不能为空");
            return null;
        }
        Map<String, Map<String, String>> getReportListAndParamMap = new HashMap<>();
        String reportId = null;
        int j = 0;
        Boolean work = true;
        while (work) {
            //发送GetReportList请求
            Map<String, Object> getReportList = AmazonMwsUntil.clientXML("GetReportList", baseParams.get(MWSURL_PARAM), baseParams.get(SECRETKEY_PARAM), baseParams);
            if (null == getReportList || null != getReportList.get(AmazonMwsUntil.ERROR_RESPONSE)) {
                log.info("发送GetReportList请求出错: getReportList" + getReportList + " 任务名称为：" + allParam.get("taskName") + " 店铺名为：" + allParam.get("shopName") + " 区域名为：" + allParam.get("areaCode"));
                return null;
            }
            if (null == getReportList.get(SUCCESS_GET_REPORT_LIST_RESPONSE)) {
                log.info("发送GetReportList请求出错: getReportList:GetReportListResponse" + getReportList + " 任务名称为：" + allParam.get("taskName") + " 店铺名为：" + allParam.get("shopName") + " 区域名为：" + allParam.get("areaCode"));
                return null;
            }
            Map<String, Object> map1 = (Map<String, Object>) getReportList.get(SUCCESS_GET_REPORT_LIST_RESPONSE);
            String getReportListResult = "GetReportListResult";
            if (null == map1.get(getReportListResult)) {
                log.info("发送GetReportList请求出错getReportList:GetReportListResponse:GetReportListResult" + getReportList + " 任务名称为：" + allParam.get("taskName") + " 店铺名为：" + allParam.get("shopName") + " 区域名为：" + allParam.get("areaCode"));
                return null;
            }
            Map<String, Object> map2 = (Map<String, Object>) map1.get(getReportListResult);
//            String reportInfo = "ReportInfo";
            //获取reportInfo对象，-----亚马逊当返回reportInfo只有一个的时候为map，多个的时候为数组
            Map<String, Object> reportInfoMap = (Map<String, Object>) map2.get("ReportInfo");
            if (null == reportInfoMap) {
                j++;
                try {
                    if (j <= 1) {
                        Thread.sleep(300000);
                        continue;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                if (j > 2) {
//                    work = false;
//                }
                log.info("发送GetReportList,无可用数据" + getReportList + " 任务名称为：" + allParam.get("taskName") + " 店铺名为：" + allParam.get("shopName") + " 区域名为：" + allParam.get("areaCode"));
                return new HashMap<String, Map<String, String>>(1) {{
                    put("noDataAvailable", new HashMap<>(1));
                }};
            }
//            Map<String, Object> map3;
//            if (reportInfoObject.getClass().isArray() || reportInfoObject instanceof JSONArray || reportInfoObject instanceof JSON) {
//                if (reportInfoObject instanceof JSONObject) {
//                    map3 = (Map<String, Object>) reportInfoObject;
//                } else {
//                    List<Map<String, Object>> list = (List<Map<String, Object>>) reportInfoObject;

//                    for (int i = 0; i < list.size(); i++) {
//                        map3 = list.get(i);
            String ReportRequestId1 = String.valueOf(reportInfoMap.get("ReportRequestId"));
            if (ReportRequestId.equals(ReportRequestId1)) {
                //发送GetReport请求
                reportId = String.valueOf(reportInfoMap.get("ReportId"));
                getReportListAndParamMap.put(reportId, allParam);
                return getReportListAndParamMap;
            }
//                    }
        }
//            }
//    }
        return null;
    }

    /**
     * 发送GetReport请求获取报告
     * 返回一个 Map<String,Map<String,Object>> key值为任务名称 value中有key为"reportList"值类型为List<List<String>>的集合
     *
     * @param baseParams
     * @return
     */
    public static Map<String, Map<String, Object>> sendGetReport(Map<String, String> baseParams) {
        // 存放全部的请求参数包括任务名称
        Map<String, Object> allParam = new HashMap<>(baseParams.size());
        for (String param : PUBLIC_REPORT_PARAM) {
            if (baseParams.containsKey(param)) {
                allParam.put(param, baseParams.get(param));
            }
        }
        List<String> unnecessaryParam = new ArrayList();
        // 去除非RequestReport接口所需要的参数
        for (String key : baseParams.keySet()) {
            if (!GET_REPORT_PARAM.contains(key)) {
                unnecessaryParam.add(key);
            }
        }
        for (String param : unnecessaryParam) {
            baseParams.remove(param);
        }
        if (StringUtil.isBlank(baseParams.get(MWSURL_PARAM))) {
            log.warn("mwsUrl不能为空");
            return null;
        }
        if (StringUtil.isBlank(baseParams.get(SECRETKEY_PARAM))) {
            log.warn("secretKey不能为空");
            return null;
        }
        if (null == baseParams) {
            log.warn("baseParams不能为空");
            return null;
        }
        Map<String, Map<String, Object>> reportRequestAndParamMap = new HashMap<>();
//        String ReportRequestId = null;

        //发送GetReport请求
        List<List<String>> reportList = AmazonMwsUntil.clientTXT("GetReport", baseParams.get(MWSURL_PARAM), baseParams.get(SECRETKEY_PARAM), baseParams);
        allParam.put("reportList", reportList);
        reportRequestAndParamMap.put(String.valueOf(allParam.get("taskName")), allParam);
        return reportRequestAndParamMap;
    }


    /**
     * 发送GetReportList请求获取ReportId payment报告
     *
     * @param baseParams
     * @return
     */
    public static Map<String, Object> sendGetReportListByPayment(Map<String, String> baseParams) throws ParseException {
        String nowAvailableDate = baseParams.get("nowAvailableDate");
        String sellerId = CryptoUtil.encode(baseParams.get("SellerId"));
        String area = baseParams.get("areaCode");
        // 存放公用的请求参数包括任务名称
        Map<String, Object> allParam = new HashMap<>(baseParams.size());
        for (String param : PUBLIC_REPORT_PARAM) {
            if (baseParams.containsKey(param)) {
                allParam.put(param, baseParams.get(param));
            }
        }
        List<String> unnecessaryParam = new ArrayList();
        // 去除非GetReportList接口所需要的参数
        for (String key : baseParams.keySet()) {
            if (!GET_REPORT_LIST_PARAM.contains(key)) {
                unnecessaryParam.add(key);
            }
        }
        for (String param : unnecessaryParam) {
            baseParams.remove(param);
        }
        baseParams.put("ReportTypeList.Type.1", AmazonMwsUntil.urlEncode(baseParams.get("ReportType")));
        baseParams.remove("ReportType");
        if (null == baseParams) {
            log.warn("params不能为空");
            return null;
        }
        if (StringUtil.isBlank(baseParams.get(MWSURL_PARAM))) {
            log.warn("mwsUrl不能为空");
            return null;
        }
        if (StringUtil.isBlank(baseParams.get(SECRETKEY_PARAM))) {
            log.warn("secretKey不能为空");
            return null;
        }
        Boolean work = true;
        while (work) {
            //发送GetReportList请求
            Map<String, Object> getReportList = AmazonMwsUntil.clientXML("GetReportList", baseParams.get(MWSURL_PARAM), baseParams.get(SECRETKEY_PARAM), baseParams);
            if (null == getReportList || null != getReportList.get(AmazonMwsUntil.ERROR_RESPONSE)) {
                log.info("发送GetReportList请求出错: getReportList" + getReportList + " 任务名称为：" + baseParams.get("taskName") + " 店铺名为：" + baseParams.get("shopName") + " 区域名为：" + baseParams.get("areaCode"));
                return null;
            }
            if (null == getReportList.get(SUCCESS_GET_REPORT_LIST_RESPONSE)) {
                log.info("发送GetReportList请求出错: getReportList:GetReportListResponse" + getReportList + " 任务名称为：" + baseParams.get("taskName") + " 店铺名为：" + baseParams.get("shopName") + " 区域名为：" + baseParams.get("areaCode"));
                return null;
            }
            Map<String, Object> map1 = (Map<String, Object>) getReportList.get(SUCCESS_GET_REPORT_LIST_RESPONSE);
            String getReportListResult = "GetReportListResult";
            if (null == map1.get(getReportListResult)) {
                log.info("发送GetReportList请求出错getReportList:GetReportListResponse:GetReportListResult" + getReportList + " 任务名称为：" + baseParams.get("taskName") + " 店铺名为：" + baseParams.get("shopName") + " 区域名为：" + baseParams.get("areaCode"));
                return null;
            }
            Map<String, Object> map2 = (Map<String, Object>) map1.get(getReportListResult);
            String reportInfo = "ReportInfo";
            //获取reportInfo对象，-----亚马逊当返回reportInfo只有一个的时候为map，多个的时候为数组
            Object reportInfoObject = map2.get(reportInfo);
            if (null == reportInfoObject) {
                log.info("发送GetReportList请求出错getReportList:GetReportListResponse:GetReportListResult:ReportInfo" + getReportList + " 任务名称为：" + baseParams.get("taskName") + " 店铺名为：" + baseParams.get("shopName") + " 区域名为：" + baseParams.get("areaCode"));
                return null;
            }
            Map<String, Object> map3;
            if (reportInfoObject.getClass().isArray() || reportInfoObject instanceof JSONArray || reportInfoObject instanceof JSON) {
                List<Map<String, Object>> list = (List<Map<String, Object>>) reportInfoObject;
                String availableDate = null;
                map3 = list.get(0);
                availableDate = DateUtil.ISO8601ToGMT(String.valueOf(map3.get("AvailableDate")));
                if (StringUtils.isEmpty(nowAvailableDate)) {
                    nowAvailableDate = DateUtil.dateToFutureDate(availableDate, -1);
                }
                // 第一个时间是目前以获取的最大的时间，若第一个时间大于目前表中的最大时间，则保存下来,获取其报告，反之则保留参数
//                if (DateUtil.compateTime(nowAvailableDate, availableDate) == -1) {
//                    Integer num = mwsPaymentRecordEverydayMapper.selectBySellerIdAndAreaNum(sellerId, area);
//                    if (num.equals(1)) {
//                        mwsPaymentRecordEverydayMapper.updateBySellerIdAndArea(sellerId, area, String.valueOf(map3.get("ReportId")), String.valueOf(map3.get("ReportRequestId")), availableDate);
//                    }
//                    allParam.put(REPORT_ID, String.valueOf(map3.get("ReportId")));
//                }
                allParam.put(PARAM_MAP, map3);
                return allParam;
            }
        }
        return allParam;
    }


}


